<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="积分商城订单ID" prop="scoreMallOrderId">
        <el-input
          v-model="queryParams.scoreMallOrderId"
          placeholder="请输入积分商城订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员ID表主键" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入会员ID表主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总积分" prop="totalScoreAmount">
        <el-input
          v-model="queryParams.totalScoreAmount"
          placeholder="请输入总积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="totalPayAmount">
        <el-input
          v-model="queryParams.totalPayAmount"
          placeholder="请输入总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易流水号" prop="transactionNo">
        <el-input
          v-model="queryParams.transactionNo"
          placeholder="请输入交易流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除标志(0：未删除 1：已删除)" prop="yn">
        <el-input
          v-model="queryParams.yn"
          placeholder="请输入删除标志(0：未删除 1：已删除)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:mallOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:mallOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:mallOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:mallOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="积分商城订单表主键" align="center" prop="id" />
      <el-table-column label="积分商城订单ID" align="center" prop="scoreMallOrderId" />
      <el-table-column label="会员ID表主键" align="center" prop="customerId" />
      <el-table-column label="总积分" align="center" prop="totalScoreAmount" />
      <el-table-column label="总金额" align="center" prop="totalPayAmount" />
      <el-table-column label="交易流水号" align="center" prop="transactionNo" />
      <el-table-column label="汇付支付宝跳转地址" align="center" prop="payUrl" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付类型(0微信 1支付宝 2银行卡)" align="center" prop="payType" />
      <el-table-column label="状态(0未支付 1已支付 2已退款 3已取消 4未完成)" align="center" prop="status" />
      <el-table-column label="删除标志(0：未删除 1：已删除)" align="center" prop="yn" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:mallOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:mallOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分商城订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="积分商城订单ID" prop="scoreMallOrderId">
          <el-input v-model="form.scoreMallOrderId" placeholder="请输入积分商城订单ID" />
        </el-form-item>
        <el-form-item label="会员ID表主键" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入会员ID表主键" />
        </el-form-item>
        <el-form-item label="总积分" prop="totalScoreAmount">
          <el-input v-model="form.totalScoreAmount" placeholder="请输入总积分" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalPayAmount">
          <el-input v-model="form.totalPayAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="交易流水号" prop="transactionNo">
          <el-input v-model="form.transactionNo" placeholder="请输入交易流水号" />
        </el-form-item>
        <el-form-item label="汇付支付宝跳转地址" prop="payUrl">
          <el-input v-model="form.payUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志(0：未删除 1：已删除)" prop="yn">
          <el-input v-model="form.yn" placeholder="请输入删除标志(0：未删除 1：已删除)" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">积分商城订单明细表信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddScoreMallOrderDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteScoreMallOrderDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="scoreMallOrderDetailList" :row-class-name="rowScoreMallOrderDetailIndex" @selection-change="handleScoreMallOrderDetailSelectionChange" ref="scoreMallOrderDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="积分商城订单明细ID" prop="scoreMallOrderDetailId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.scoreMallOrderDetailId" placeholder="请输入积分商城订单明细ID" />
            </template>
          </el-table-column>
          <el-table-column label="商品ID" prop="goodsId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsId" placeholder="请输入商品ID" />
            </template>
          </el-table-column>
          <el-table-column label="支付规则 (0：积分 1：现金 2：积分+现金）" prop="goodsPayRule" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsPayRule" placeholder="请输入支付规则 (0：积分 1：现金 2：积分+现金）" />
            </template>
          </el-table-column>
          <el-table-column label="商品积分数" prop="goodsScore" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsScore" placeholder="请输入商品积分数" />
            </template>
          </el-table-column>
          <el-table-column label="商品现金" prop="goodsMoney" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsMoney" placeholder="请输入商品现金" />
            </template>
          </el-table-column>
          <el-table-column label="商品数量" prop="goodsAmount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsAmount" placeholder="请输入商品数量" />
            </template>
          </el-table-column>
          <el-table-column label="删除标志(0：未删除 1：已删除)" prop="yn" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.yn" placeholder="请输入删除标志(0：未删除 1：已删除)" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMallOrder, getMallOrder, delMallOrder, addMallOrder, updateMallOrder } from "@/api/customer/mallOrder";

export default {
  name: "MallOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedScoreMallOrderDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 积分商城订单表格数据
      mallOrderList: [],
      // 积分商城订单明细表表格数据
      scoreMallOrderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scoreMallOrderId: null,
        customerId: null,
        totalScoreAmount: null,
        totalPayAmount: null,
        transactionNo: null,
        payUrl: null,
        payTime: null,
        payType: null,
        status: null,
        yn: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        transactionNo: [
          { required: true, message: "交易流水号不能为空", trigger: "blur" }
        ],
        payUrl: [
          { required: true, message: "汇付支付宝跳转地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询积分商城订单列表 */
    getList() {
      this.loading = true;
      listMallOrder(this.queryParams).then(response => {
        this.mallOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scoreMallOrderId: null,
        customerId: null,
        totalScoreAmount: null,
        totalPayAmount: null,
        transactionNo: null,
        payUrl: null,
        payTime: null,
        payType: null,
        status: null,
        yn: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.scoreMallOrderDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分商城订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMallOrder(id).then(response => {
        this.form = response.data;
        this.scoreMallOrderDetailList = response.data.scoreMallOrderDetailList;
        this.open = true;
        this.title = "修改积分商城订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.scoreMallOrderDetailList = this.scoreMallOrderDetailList;
          if (this.form.id != null) {
            updateMallOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除积分商城订单编号为"' + ids + '"的数据项？').then(function() {
        return delMallOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 积分商城订单明细表序号 */
    rowScoreMallOrderDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 积分商城订单明细表添加按钮操作 */
    handleAddScoreMallOrderDetail() {
      let obj = {};
      obj.scoreMallOrderDetailId = "";
      obj.goodsId = "";
      obj.goodsPayRule = "";
      obj.goodsScore = "";
      obj.goodsMoney = "";
      obj.goodsAmount = "";
      obj.yn = "";
      obj.remark = "";
      this.scoreMallOrderDetailList.push(obj);
    },
    /** 积分商城订单明细表删除按钮操作 */
    handleDeleteScoreMallOrderDetail() {
      if (this.checkedScoreMallOrderDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的积分商城订单明细表数据");
      } else {
        const scoreMallOrderDetailList = this.scoreMallOrderDetailList;
        const checkedScoreMallOrderDetail = this.checkedScoreMallOrderDetail;
        this.scoreMallOrderDetailList = scoreMallOrderDetailList.filter(function(item) {
          return checkedScoreMallOrderDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleScoreMallOrderDetailSelectionChange(selection) {
      this.checkedScoreMallOrderDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/mallOrder/export', {
        ...this.queryParams
      }, `mallOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
